fn x_to_ten(num: &str, base: u32) -> usize {
    let mut ans: usize = 0;
    for x in num.chars() {
        ans = ans * base as usize + to_num(x);
    }
    ans
}

fn ten_to_x(mut num: usize, base: u32) -> String {
    let mut ans = String::new();
    while num != 0 {
        ans += to_str(num % base as usize).as_str();
        num /= base as usize;
    }
    ans.chars().rev().collect()
}

fn to_num(x: char) -> usize {
    if x.is_numeric() {
        x.to_string().parse().unwrap()
    } else {
        match x {
            'a' => 10,
            'b' => 11,
            'c' => 12,
            'd' => 13,
            'e' => 14,
            'f' => 15,
            'g' => 16,
            'h' => 17,
            'i' => 18,
            'j' => 19,
            'k' => 20,
            'l' => 21,
            'm' => 22,
            'n' => 23,
            'o' => 24,
            'p' => 25,
            'q' => 26,
            'r' => 27,
            's' => 28,
            't' => 29,
            'u' => 30,
            'v' => 31,
            'w' => 32,
            'x' => 33,
            'y' => 34,
            'z' => 35,
            _ => panic!(),
        }
    }
}

fn to_str(num: usize) -> String {
    if num < 10 {
        num.to_string()
    } else {
        match num {
            10 => "a",
            11 => "b",
            12 => "c",
            13 => "d",
            14 => "e",
            15 => "f",
            16 => "g",
            17 => "h",
            18 => "i",
            19 => "j",
            20 => "k",
            21 => "l",
            22 => "m",
            23 => "n",
            24 => "o",
            25 => "p",
            26 => "q",
            27 => "r",
            28 => "s",
            29 => "t",
            30 => "u",
            31 => "v",
            32 => "w",
            33 => "x",
            34 => "y",
            35 => "z",
            _ => panic!(),
        }
        .to_string()
    }
}

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let input: Vec<_> = num_str.trim_end_matches(")").split("(").collect();
    let num = x_to_ten(input[0], input[1].parse().unwrap());
    ten_to_x(num, to_base)
}
